/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.onlineresortreservation.helper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.onlineresortreservation.entity.Event;
import org.onlineresortreservation.connection.Dbcon;
import org.onlineresortreservation.entity.Addontype;
import org.onlineresortreservation.entity.CancellationPolicy;
import org.onlineresortreservation.entity.GalleryType;
/**
 *
 * @author Jennifer
 */
public class Admin {

    Dbcon dbcon = new Dbcon();
    Connection con;
    Statement stmt;
    PreparedStatement psmt;
    ResultSet rs;
    String sql;

    public void AddEvent(Event e) {


        try {
            String eventname = e.getEventname();
            String eventdate = e.getEventdate();
            String tp = eventdate.toString();
            String eventtime = e.getEventtime();
            int noofecopasses = e.getTotalecopass();
            int ecoprice = e.getEcopassprice();
            int noofvippasses = e.getTotalvippass();
            int vipprice = e.getVippassprice();
            String eventvenue = e.getEventplace();
            String eventimage = e.getImagepath();
            String eventdescription = e.getEventdescription();
            int eventcheckboxvalue;

            String eventcheckbox = e.getEventcheckbox();

            if (eventcheckbox == null) {
                eventcheckboxvalue = 0;
            } else {
                eventcheckboxvalue = 1;
            }
            con = dbcon.open();
            sql = "Insert into events (EventName, EventDate, EventTime, EventPlace, EventDescription, ActiveStatus, ImagePath, TotalVipPass, TotalEcoPass, VipPassPrice, EcoPassPrice) values(?,?,?,?,?,?,?,?,?,?,?);";
            psmt = con.prepareCall(sql);

            psmt.setString(1, eventname);
            psmt.setString(2, eventdate);
            psmt.setString(3, eventtime);
            psmt.setString(4, eventvenue);
            psmt.setString(5, eventdescription);
            psmt.setInt(6, eventcheckboxvalue);
            psmt.setString(7, eventimage);
            psmt.setInt(8, noofvippasses);
            psmt.setInt(9, noofecopasses);
            psmt.setInt(10, vipprice);
            psmt.setInt(11, ecoprice);

            //System.out.println(sql);

            int count = psmt.executeUpdate();
            //System.out.println(count);

            dbcon.conclose();
        } catch (Exception exc) {
            String excmsg = exc.getMessage();
            System.out.println(excmsg);
        }
    }

    public void AddAddonType(Addontype add) {

        try {
            String addontypename = add.getAddonTypeName();
            String addontypedescription = add.getAddonTypeDescription();
            String addontypestatus = add.getAddonTypeCheckbox();
            int addontypestatusvalue;

            if (addontypestatus.equals("active")) {
                addontypestatusvalue = 1;
            } else {
                addontypestatusvalue = 0;
            }

            con = dbcon.open();
            String sql1 = "Insert into resortreservation.addontype (AddonTypeName, AddonTypeDesc, Activestatus) values(\"" + addontypename + "\",\"" + addontypedescription + "\", \"" + addontypestatus + "\")";
            psmt = con.prepareCall(sql1);

            psmt.setString(1, addontypename);
            psmt.setString(2, addontypedescription);
            psmt.setString(3, addontypestatus);

            int count = psmt.executeUpdate();
            System.out.println(count);
            dbcon.conclose();

        } catch (Exception exc) {
            String excmsg = exc.getMessage();
            System.out.println(excmsg);
        }
    }

    public List<Event> ListActiveEvents() {
        List allEvents = new ArrayList();
        try {
            String sql = "Select EventId, EventName, EventDate, EventTime, EventPlace, EventDescription FROM events where ActiveStatus=1 or ActiveStatus=0";
            con = dbcon.open();
            stmt = con.createStatement();
            rs = stmt.executeQuery(sql);

        } catch (Exception exc) {
            String excmsg = exc.getMessage();
            System.out.println(excmsg);
        }

        try {
            if (rs.next()) {
                do {
                    int id = Integer.parseInt(rs.getString("EventId"));
                    String name = rs.getString("EventName");
                    String date = rs.getString("EventDate");
                    String time = rs.getString("EventTime");
                    String location = rs.getString("EventPlace");
                    String desc = rs.getString("EventDescription");

                    Event e = new Event(id, name, date, time, location, desc);
                    allEvents.add(e);
                } while (rs.next());
                dbcon.conclose();
            }
        } catch (Exception exc) {
            String excmsg = exc.getMessage();
            System.out.println(excmsg);
        }

        return allEvents;

    }

    public Event getEventDetails(int eventid) {
        Event e = null;
        try {
            String sql = "Select EventId, EventName, EventDate, EventTime, EventPlace, EventDescription, ActiveStatus, ImagePath, TotalVipPass,TotalEcoPass, VipPassPrice, EcoPassPrice FROM events where Eventid=?";
            con = dbcon.open();
            psmt = con.prepareStatement(sql);
            psmt.setInt(1, eventid);
            rs = psmt.executeQuery();
        } catch (Exception exc) {
            String excmsg = exc.getMessage();
            System.out.println(excmsg);
        }
        try {
            if (rs.next()) {
                do {
                    int id = Integer.parseInt(rs.getString("EventId"));
                    String name = rs.getString("EventName");
                    String date = rs.getString("EventDate");
                    String time = rs.getString("EventTime");
                    String location = rs.getString("EventPlace");
                    String desc = rs.getString("EventDescription");
                    String activestatus = rs.getString("ActiveStatus");
                    String imagepath = rs.getString("ImagePath");
                    int totalvippass = Integer.parseInt(rs.getString("TotalVipPass"));
                    int totalecopass = Integer.parseInt(rs.getString("TotalVipPass"));
                    int vippassprice = Integer.parseInt(rs.getString("TotalVipPass"));
                    int ecopassprice = Integer.parseInt(rs.getString("TotalVipPass"));

                    e = new Event(id, name, date, time, location, imagepath, totalvippass, totalecopass, vippassprice, ecopassprice, desc, activestatus);

                } while (rs.next());
                dbcon.conclose();
            }

        } catch (Exception exc) {
            String excmsg = exc.getMessage();
            System.out.println(excmsg);
        }
        return e;
    }
     
    public void DeleteEvent(int eventid) {
        try {
            sql = "Update events SET ActiveStatus=2 where eventid=? ";
            con = dbcon.open();
            psmt = con.prepareStatement(sql);
            psmt.setInt(1, eventid);

            int count = psmt.executeUpdate();
            dbcon.conclose();
        } catch (Exception exc) {
            String excmsg = exc.getMessage();
            System.out.println(excmsg);
        }

    }
}

    public void addCencellationPolicy(CancellationPolicy cp)
    {
        try
            {
                int count=-1;
                int s;
                int days=cp.getNoOfDays();
                int percentage=cp.getPercentage();
                java.util.Date date=cp.getFromEffectiveDate();
                java.sql.Date sqld=new java.sql.Date(date.getTime());
                
                con=dbcon.open();
                String insertquery="Insert into resortreservation.CancellationPolicy (Percentage, NoOfDays, PolicyEffectiveFrom) values(?,?,?)";
                
                psmt=con.prepareCall(insertquery);
                
                psmt.setInt(1,percentage);
                psmt.setInt(2,days);
                psmt.setDate(3,sqld);
                
                s=psmt.executeUpdate();
                System.out.println(count);
                dbcon.conclose();
               if(count==-1)
               {
                   String result="Cancellation policy not yet defined";
               }
               else
               {
                   String result="Cancellation policy defined successfully !!!";
               }
                dbcon.conclose();
                
                }
          catch(Exception exc)
            {
                String excmsg=exc.getMessage();
                System.out.println(excmsg);
            }
    }   
    
    
     public void AddGalleryType(GalleryType gallerytype)
     {
    
          try
            {
                String gallerytypename=gallerytype.getGalleryTypeName();
                                
                con=dbcon.open();
                String sql1="Insert into resortreservation.gallerytype (GalleryTypeName) values(\""+gallerytypename +"\")";
                Statement st;                
                st = con.createStatement();
                st.executeUpdate(sql1);              
             }
          catch(Exception exc)
            {
                String excmsg=exc.getMessage();
                System.out.println(excmsg);
            }
    }
     
     
    
    
   }
